package leetcode_acm.array;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年03月17日 14:43
 */
public class SearchRange {
    public int[] searchRange(int[] nums, int target) {
        int leftBound = getLeftBound(nums, target);
        int rightBound = getRightBound(nums, target);
        return new int[]{leftBound,rightBound};
    }
    private int getLeftBound(int[] nums,int target){
        if(nums == null || nums.length == 0){
            return -1;
        }
        int leftBound = -1;
        int left = 0,right = nums.length;
        while(left <= right){
            int mid = left+((right-left)>>1);
            if(nums[mid] == target){
                leftBound = mid;
                right = mid-1;
            }else if(nums[mid] > target){
                right = mid-1;
            }else if(nums[mid] < target){
                left = mid+1;
            }
        }
        return leftBound;
    }
    private int getRightBound(int[] nums,int target){
        if(nums == null || nums.length == 0){
            return -1;
        }
        int rightBound = -1;
        int left = 0,right = nums.length-1;
        while(left <= right){
            int mid = left+((right-left)>>1);
            if(nums[mid] == target){
                rightBound = mid;
                left = mid+1;
            }else if(nums[mid] > target){
                right = mid-1;
            }else if(nums[mid] < target){
                left = mid+1;
            }
        }
        return rightBound;
    }
}
